package com.nova.edc.service.system.impl;

import com.nova.edc.dao.system.AreaPointDao;
import com.nova.edc.dao.system.DeviceAirDao;
import com.nova.edc.dao.system.PointAirDao;
import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.model.system.DeviceAir;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.model.system.PointAir;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.PointAirService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.module.filter.query.Filter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class PointAirServiceImpl implements PointAirService {
    @Autowired
    private PointAirDao pointAirDao;

    @Autowired
    private DeviceAirDao deviceAirDao;

    @Autowired
    private AreaPointDao areaPointDao;

    @Autowired
    private AreaPointService areaPointService;

    @Override
    public PointAir insert(PointAir pointAir) {
        DeviceAir deviceAir = new DeviceAir();
        deviceAir.setAircode(pointAir.getAircode());
        pointAir.setAirid(deviceAirDao.findByExample(deviceAir).get(0).getId());

        int realpower = areaPointDao.getById(String.valueOf(pointAir.getPointid())).getRealpower();
        int power = deviceAirDao.getById(pointAir.getAirid()).getPower();
        realpower = realpower + power * pointAir.getAirnum();
        AreaPoint areaPoint = new AreaPoint();
        areaPoint.setRealpower(realpower);
        areaPoint.setId(pointAir.getPointid());
        areaPointDao.updateNotNull(areaPoint);
        return pointAirDao.insert(pointAir);
    }

    @Override
    public int delete(PointAir pointAir) {
        return pointAirDao.deleteByExample(pointAir);
    }

    @Override
    public int deleteById(int id) {
        PointAir pointAir = pointAirDao.getById(String.valueOf(id));
        areaPointService.updatePower(ModelConstant.AIR_AIRTYPE, pointAir.getPointid(), 0 - pointAir.getAirnum(), pointAir.getAirid());
        return pointAirDao.deleteById(String.valueOf(id));
    }

    @Override
    public int update(PointAir pointAir) {
        PointAir lastpointAir = pointAirDao.getById(String.valueOf(pointAir.getId()));
        areaPointService.updatePower(ModelConstant.AIR_AIRTYPE, pointAir.getPointid(), 0 - lastpointAir.getAirnum(), lastpointAir.getAirid());
        DeviceAir deviceAir = new DeviceAir();
        deviceAir.setAircode(pointAir.getAircode());
        pointAir.setAirid(deviceAirDao.findByExample(deviceAir).get(0).getId());
        areaPointService.updatePower(ModelConstant.AIR_AIRTYPE, pointAir.getPointid(), pointAir.getAirnum(), pointAir.getAirid());
        return pointAirDao.updateNotNull(pointAir);
    }

    @Override
    public List<PointAir> findAll() {
        return pointAirDao.findAll();
    }

    @Override
    public PageList<PointAir> findPointAirPageList(PointAir pointAir,
                                                   Page page, String sidx, String sord) {
        Query<PointAir> query = pointAirDao.query().addOrder(
                Order.custom(sord, sidx));
        Filter.addFilterCondition(query, pointAir);
        return query.pageList(page);
    }

    @Override
    public PointAir findById(int id) {
        return pointAirDao.getById(String.valueOf(id));
    }

    @Override
    public List<PointAir> findExample(PointAir pointAir) {
        return pointAirDao.findByExample(pointAir);
    }
}
